/*
 * InvulgoTracker
 * Copyright (C) Thomas Andersen 2011 <thomas.1.andersen@gmail.com>
 * 
 * InvulgoTracker is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * InvulgoTracker is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TASK_VIEW_H_
#define _TASK_VIEW_H_

#include <gtk/gtk.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xmlwriter.h>
#include "taskid.h"
#include "prefs.h"
#include <list>

using std::list;


class TaskView
{
public:
    TaskView();
    virtual ~TaskView();
    void Init(GtkWidget *view);
    void Restart();
    void Save();
    void Add();
    void Delete();
    void AddRoot();
    void Edit(gchar *pathStr, gchar *newStr);
    bool GetSelected(gchar **name, TaskId &id);
    bool Get(TaskId id, gchar **name);


    /**
      * Return item pointed to by path.
      */
    bool Get(GtkTreePath *path, gchar **name, TaskId &id);

    /**
      * Return parent id and name of supplied id.
      */
    bool GetParent(TaskId &id, gchar **name);

    /**
      * Build menu containing all tasks.
      */
    void BuildMenu(GtkWidget *parentItem, GCallback callback, list<TaskId *> &menuTaskList);

    GtkTreeModel *GetModel() { return model; }

    /**
      * Place iter at task - start search from first.
      */
    gboolean FindTask(GtkTreeIter *iter, TaskId &taskId);

    /**
      * Return taskid at iter.
      */
    TaskId GetTaskId(GtkTreeIter *iter);

    GtkWidget *GetCurrentDlg() { return currentDlg; }

    /**
      * Return best case value.
      */
    glong GetBestCase(TaskId &id);
    /**
      * Return most likely value.
      */
    glong GetMostLikely(TaskId &id);
    /**
      * Return worst case value.
      */
    glong GetWorstCase(TaskId &id);
    /**
      * Set best case.
      * @param id Task id.
      * @param bestCase best case hours.
      */
    void SetBestCase(TaskId &id, glong bestCase);
    /**
      * Set most likely.
      * @param id Task id.
      * @param mostLikely most likely hours.
      */
    void SetMostLikely(TaskId &id, glong mostLikely);
    /**
      * Set worst case.
      * @param id Task id.
      * @param worstCase worst case hours.
      */
    void SetWorstCase(TaskId &id, glong worstCase);

    /**
      * Return 3-point 95% confidence estimate.
      */
    glong GetEstimate95(TaskId &id);

    /**
      * Return deadline.
      */
    glong GetDeadline(TaskId &id);
    /**
      * Return hasDeadline.
      */
    gboolean GetHasDeadline(TaskId &id);
    /**
      * Set deadline.
      */
    void SetDeadline(TaskId &id, glong deadline);
    /**
      * Set hasDeadline.
      */
    void SetHasDeadline(TaskId &id, gboolean hasDeadline);
    /**
      * Set Done.
      */
    void SetDone(TaskId &id, gboolean done);
    /**
      * Return done.
      */
    gboolean GetDone(TaskId &id);
    /**
      * Refilter model.
      */
    void Refilter();

    void view_popup_menu(GtkWidget *treeview, GdkEventButton *event, gpointer userdata);
protected:

private:
    void Load();
    void Clean();
    /**
      * Write taskdata under parent to file.
      */
    void StoreTask(xmlTextWriterPtr writer, GtkTreeIter *parent, TaskId parid);
    void ParseTask(xmlDocPtr doc, xmlNodePtr cur);
    /**
      * Place iter at taskid - start from current position.
      */
    gboolean FindTaskFromPos(TaskId &parid, GtkTreeIter *iter);
    void BuildSubMenu(GtkTreeIter *parent, GtkWidget *parentItem, GCallback callback, list<TaskId *> &menuTaskList);
    void QualifyParentNames(GtkTreeIter *iter, gchar **name);
    bool WarnDelete();
    static gboolean FilterFunc(GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
    static void view_popup_menu_onDoEditName(GtkWidget *menuitem, gpointer userdata);
    static void view_popup_menu_onMarkDone(GtkWidget *menuitem, gpointer userdata);
    void UpdateColor(GtkTreeIter *iter);

    enum
    {
      COL_NAME = 0,
      COL_ID,
      COL_BEST,
      COL_LIKELY,
      COL_WORST,
      COL_DEADLINE,
      COL_HASDEADLINE,
      COL_DONE,
      COL_COLOR,
      NUM_COLS
    } ;

    GtkTreeView *view;
    GtkTreeModel *model;
    GtkTreeModel *modelFilter;
    GtkWidget *currentDlg;
    Prefs       *prefs;
};

#endif // _TASK_VIEW_H_
